package com.atguigu.FIleIOExer;

import org.junit.Test;

import java.io.*;

/**
 * 比较字节的节点流和缓存流的速度差别FileInputStream,FileOutputStream,缓存流：BufferedInputStream,BufferedOutputStream在读入读出的差别
 * @author xiaoqi
 * @data 2021/04/20 10:28
 */
class FileIOThread1 extends Thread {
    public  void ByteDemo(){
        long starttime = System.currentTimeMillis();
        //设置字节输入输出流
        FileInputStream fr = null;
        FileOutputStream fw = null;
        byte[] ch = new byte[1000];
        //异常测试
        try{
//            fr = new FileReader(new File("D:\\Program Files (x86)\\qq2312762680\\test1.txt"));
//            fw = new FileWriter(new File("D:\\Googl浏览器下载\\largeTest_bytes.txt"));
            fr = new FileInputStream(new File("D:\\Googl浏览器下载\\尚学堂_爬虫\\45_尚学堂_scrapy爬取小说_Python网络爬虫实战.mp4"));
            fw = new FileOutputStream(new File("E:\\python爬虫2.mp4"));
            int flag;
            while((flag = fr.read(ch)) != -1){
                //写入数据
                fw.write(ch);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //关闭IO流
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long endtime = System.currentTimeMillis();
        System.out.printf(" Byte starttime:%d, endime:%d, lasttime:%d",starttime, endtime, endtime - starttime);
        System.out.println();
    }


    @Override
    public void run() {
        Thread.currentThread().setName("字节流线程");
        this.ByteDemo();
    }
}
class FileThread2 extends Thread{
    public  void CacheDemo(){
        long starttime = System.currentTimeMillis();
        //设置字节输入输出流
        BufferedInputStream br = null;
        BufferedOutputStream bw = null;
        byte[] by = new byte[1000];
        int len;
        //异常测试
        try{
//            br = new BufferedReader(new FileReader("D:\\Googl浏览器下载\\largeTest.txt"));
//            bw = new BufferedWriter(new FileWriter("D:\\Googl浏览器下载\\largeTest_Cache.txt"));
            br = new BufferedInputStream(new FileInputStream(new File("D:\\Googl浏览器下载\\尚学堂_爬虫\\45_尚学堂_scrapy爬取小说_Python网络爬虫实战.mp4")));
            bw = new BufferedOutputStream(new FileOutputStream(new File("E:\\python爬虫1.mp4")));
            while((len = br.read(by)) != -1){
                //写入数据
                bw.write(by);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //关闭IO流
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ;
        long endtime = System.currentTimeMillis();
        System.out.printf("Cache starttime:%d, endime:%d, lasttime:%d",starttime, endtime, endtime - starttime);
        System.out.println();
    }

    @Override
    public void run() {
        this.CacheDemo();
    }
}
public class IOCompared {
    @Test
    public void test(){
        System.out.println('A' ^ 'a');
    }
    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        //字节流线程
        FileIOThread1 file = new FileIOThread1();
        file.ByteDemo();
//        file.start();
        System.out.println("-----------------分割线------------------");
        FileThread2 file2 = new FileThread2();
        file2.CacheDemo();
//        file2.start();
//        while(true){
//            //根据进程的状态来判断进程是否完完全结束
//            if(file.getState() == Thread.State.TERMINATED && file2.getState() == Thread.State.TERMINATED){
//                break;
//            }
            long end = System.currentTimeMillis();
            System.out.printf("%s starttime:%d, endime:%d, lasttime:%d\n",Thread.currentThread().getName(), start, end, end - start);

    }
}
